package com.zr.legalfactory.controller;

import com.zr.legalfactory.pojo.*;
import com.zr.legalfactory.service.LegalFactoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by 陈紫陌 on 2019-10-08.
 */
@RestController
@Slf4j
@CrossOrigin
public class LegalFactoryController {
    @Autowired
    private LegalFactoryService legalFactoryService;
    /*
    分页查询
     */
    @PostMapping("/legalPlant/query")
    public ResultVo query(@RequestBody @Valid PageVo pageVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVoBuild.error("500", bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = legalFactoryService.query(pageVo);
        return  resultVo;
    }
    /*
        法人下拉框
     */
    @GetMapping("/legalPerson/ofCurrentUser")
    public ResultVo ofCurrentUser(HttpServletRequest request){
//        模拟登陆(默认登陆成功)
        request.getSession().setAttribute("username", "zhaosi");
        String username = (String) request.getSession().getAttribute("username");
        ResultVo resultVo = legalFactoryService.ofCurrentUser(username);
        return resultVo;
    }
    /*
    新增
     */
    @PostMapping("/legalPlant/add")
    public ResultVo add(@RequestBody @Valid AddVo addVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVoBuild.error("500", bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = legalFactoryService.add(addVo);
        return resultVo;
    }
    /*
    修改
     */
    @PostMapping("/legalPlant/update")
    public ResultVo update(@RequestBody @Valid UpdateVo updateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVoBuild.error("500", bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = legalFactoryService.update(updateVo);
        return resultVo;
    }
    //    启用禁用修改
    @PostMapping("/legalPlant/updatestatus")
    public ResultVo updatestatus(@RequestBody OnOffVo onOffVo){
        ResultVo resultVo = legalFactoryService.updatestatus(onOffVo);
        return resultVo;
    }

    //demo
    @PostMapping("/queryBo")
    public CorporateBo queryBo(int id){
        CorporateBo corporateBo = legalFactoryService.queryBo(id);
        return corporateBo;
    }
    @PostMapping("/queryList")
    public List<CorporateBo> queryList(@RequestBody List<Integer> ids){
        List<CorporateBo> list = legalFactoryService.queryList(ids);
        return list;
    }
    @PostMapping("legalfactory/importFile")
    public ResultVo importFile(MultipartFile file) throws Exception {
        ResultVo resultVo = legalFactoryService.importFile(file);
        return resultVo;
    }
}
